﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;
using System;

public class FUIMain : MonoBehaviour
{
    static FUIMain _instance;
    GComponent _mainView;

    public static Controller _funcCtrl;
    GComponent _content;
    Dictionary<GMConfig.PanelType, MyWindow> _windowDict;
    List<MyWindow> _mutexDict;
    Dictionary<string, GComponent> _componentDict;

    void Awake() {
        _instance = this;
        UIConfig.defaultFont = "NSimSun";
        UIPackage.AddPackage("UI/CommonPackage");
        UIPackage.AddPackage("UI/CardImgPackage");
        UIPackage.AddPackage("UI/CardPackage");
        UIPackage.AddPackage("UI/EquipmentPackage");
        UIPackage.AddPackage("UI/BiologyPackage");
        UIPackage.AddPackage("UI/MapPackages");
        UIPackage.AddPackage("UI/AudioPackage");
        UIPackage.AddPackage("UI/ItemPackage");
        UIPackage.AddPackage("UI/ItemImgPackage");
        UIPackage.AddPackage("UI/WeaponImgPackage");
        //脚步绑定
        UIObjectFactory.SetPackageItemExtension("ui://CommonPackage/CardAndDiscript", typeof(FCard));
        UIObjectFactory.SetPackageItemExtension("ui://CommonPackage/WeaponAndDiscript", typeof(FWeapon));
        UIObjectFactory.SetPackageItemExtension("ui://CommonPackage/BiologyAndDiscript", typeof(FBiology));
        UIObjectFactory.SetPackageItemExtension("ui://CommonPackage/NormalListItem", typeof(FSuit));

        _componentDict = new Dictionary<string, GComponent>();
        _windowDict = new Dictionary<GMConfig.PanelType, MyWindow>();
        _mutexDict = new List<MyWindow>();
    }
    // static public object ChangeType(object value, Type type)
    // {
    //     if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
    //     if (value == null) return null;
    //     if (type == value.GetType()) return value;
    //     if (type.IsEnum)
    //     {
    //         if (value is string)
    //             return Enum.Parse(type, value as string);
    //         else
    //             return Enum.ToObject(type, value);
    //     }
    //     if (!type.IsInterface && type.IsGenericType)
    //     {
    //         Type innerType = type.GetGenericArguments()[0];
    //         object innerValue = ChangeType(value, innerType);
    //         return Activator.CreateInstance(type, new object[] { innerValue });
    //     }
    //     if (!(value is IConvertible)) return value;
    //     return Convert.ChangeType(value, type);
    // }
    // Use this for initialization
    void Start() {
        _mainView = GetComponent<UIPanel>().ui;
        _content = _mainView.GetChild("content").asCom;

        //new FCardListMediator().ShowOn(_content.root);
        //卡牌列表
        _funcCtrl = _mainView.GetController("funcCtrl");
        _funcCtrl.onChanged.Add(OnFuncLevelChange);
        _funcCtrl.selectedIndex = 1;

        var t = new Test();
        var str = LitJson.JsonMapper.ToJson(t);
        // Debug.Log(str);
        // var a = new JsonTest();
        var d = LitJson.JsonMapper.ToObject<object>(str);
        var e = d as Test;
        //var e = Utils.ConverToType<object>(d);
        JsonTest b = new Test() as JsonTest;
        var c = b.c;

        // _mainView.GetChild("copyBtn").asButton.onClick.Add(() => {
        //     // using (System.IO)
        //     // {
                
        //     // }
        // });
    }
    // Update is called once per frame
    void Update() {

    }
    private void OnFuncLevelChange(EventContext context)
    {
        MyWindow panel = null;
        switch (_funcCtrl.selectedIndex)
        {
            case 0:
                panel = GetWindow(GMConfig.PanelType.EditMapPanel);
                break;
            case 1:
                panel = GetWindow(GMConfig.PanelType.CardList);
                break;
            case 2:
                panel = GetWindow(GMConfig.PanelType.BiologyListPanel);
                break;
            case 3:
                panel = GetWindow(GMConfig.PanelType.RoomPanel);
                break;
            case 4:
                panel = GetWindow(GMConfig.PanelType.WeaponList);
                break;
            case 5:
                panel = GetWindow(GMConfig.PanelType.SuitList);
                break;
            case 6:
                panel = GetWindow(GMConfig.PanelType.ItemList);
                break;
            default:
                break;
        }
        //隐藏其它面板
        foreach(var item in _mutexDict){
            if(item.visible) _content.RemoveChild(item);
        }
        if(panel != null){
            if(!_mutexDict.Contains(panel)) {
                _mutexDict.Add(panel);
            } 
            _content.AddChild(panel);            
            panel.size = _content.size;
            panel.position = Vector3.zero;
            panel.bringToFontOnClick = false;//禁止放到前面
            if(panel.dragArea != null) panel.dragArea.draggable = false;//禁止拖动
        }
    }
    /// <summary>
    /// 提示面板
    /// </summary>
    /// <param name="text"></param>
    /// <param name="cb"></param>
    public static void TipPanel(string text, OneDelegate cb = default(OneDelegate)) {
        MyWindow panel = null;
        if(cb == null) panel = ShowPopupPanel(GMConfig.PanelType.TipPanel);
        else panel = ShowPanel(GMConfig.PanelType.TipPanel);
        ((FTipPanelMediator)panel).Tip(text, cb);
    }
    /// <summary>
    /// 输入框
    /// </summary>
    /// <param name="val"></param>
    /// <param name="cb"></param>
    public static void InputPanel(string val, TwoDelegate cb)
    {
        var inputPanel = ShowPanel(GMConfig.PanelType.InputPanel);
        ((FInputPanelMediator)inputPanel).InitData(val, cb);
    }
    /// <summary>
    /// 获得指定面板
    /// </summary>
    /// <param name="panelType"></param>
    /// <returns></returns>
    public static MyWindow GetWindow(GMConfig.PanelType panelType,OneDelegate cb = default(OneDelegate))
    {
        MyWindow panel = null;
        if (_instance._windowDict.ContainsKey(panelType))
        {
            panel = _instance._windowDict.GetValue(panelType);
        }
        else
        {
            switch (panelType)
            {
                case GMConfig.PanelType.SelecImgtPanel:
                case GMConfig.PanelType.SelectAudioPanel:
                    panel = new FSelectListMediator();
                    break;
                case GMConfig.PanelType.SelectBiologyList:
                case GMConfig.PanelType.BiologyListPanel:
                    panel = new FBiologyListMediator(cb);
                    break;
                case GMConfig.PanelType.SelectCardList:
                case GMConfig.PanelType.CardList:
                    panel = new FCardListMediator(cb);
                    break;
                case GMConfig.PanelType.SelectSuitList:
                case GMConfig.PanelType.SuitList:
                    panel = new FSuitListMediator(cb);
                    break;
                case GMConfig.PanelType.SelectWeaponList:
                case GMConfig.PanelType.WeaponList:
                    panel = new FWeaponListMediator(cb);
                    break;
                case GMConfig.PanelType.TipPanel:
                    panel = new FTipPanelMediator();
                    break;
                case GMConfig.PanelType.InputPanel:
                    panel = new FInputPanelMediator();
                    break;
                case GMConfig.PanelType.EditCardPanel:
                    panel = new FEditCardPanel();
                    break;
                case GMConfig.PanelType.EditSuitPanel:
                    panel = new FEditSuitMediator();
                    break;
                case GMConfig.PanelType.EditWeaponPanel:
                    panel = new FEditWeaponMediator();
                    break;
                case GMConfig.PanelType.EditBiologyPanel:
                    panel = new FEditBiologyMediator();
                    break;
                case GMConfig.PanelType.RoomPanel:
                    panel = new FRoomPanelMediator();
                    break;
                    
                case GMConfig.PanelType.SelectRoomListPopup:
                case GMConfig.PanelType.FixedRoomListPopup:
                    panel = new FFixedRoomListPopup(cb);                    
                    break;
                case GMConfig.PanelType.EditMapPanel:
                    panel = new FMapEditorMediator();
                    break;
                case GMConfig.PanelType.CommonListPanel:
                    panel = new FCommonListMediator();
                    break;
                case GMConfig.PanelType.ItemList:
                    panel = new FItemListMediator(cb);
                    break;
                default:
                    return null;
            }
            _instance._windowDict.AddOrReplace(panelType, panel);
        }
        if(panel != null)
        {
            panel.bringToFontOnClick = true;//可以放到前面
            if(panel.dragArea != null) panel.dragArea.draggable = true;//可以拖动
        }
        return panel;
    }
    /// <summary>
    /// 显示指定面板
    /// </summary>
    /// <param name="cb"></param>
    public static MyWindow ShowPanel(GMConfig.PanelType panelType, OneDelegate cb = default(OneDelegate),string msg = "")
    {
        var panel = GetWindow(panelType,cb);
        if(panel != null)
        {
            panel.Show();
        }
        return panel;
    }
    /// <summary>
    /// 关闭指定面板
    /// </summary>
    /// <param name="cb"></param>
    public static MyWindow ClosePanel(GMConfig.PanelType panelType)
    {
        var panel = GetWindow(panelType,null);
        if(panel != null)
        {
            panel.Hide();
        }
        return panel;   
    }
    /// <summary>
    /// 弹出式显示指定面板
    /// </summary>
    /// <param name="cb"></param>
    public static MyWindow ShowPopupPanel(GMConfig.PanelType panelType, OneDelegate cb = default(OneDelegate),string msg = "")
    {
        var panel = GetWindow(panelType,cb);
        if(panel != null)
        {
            GRoot.inst.ShowPopup(panel);
        }
        return panel;
    }
    /// <summary>
    /// 关闭指定弹出式面板
    /// </summary>
    /// <param name="cb"></param>
    public static MyWindow ClosePopupPanel(GMConfig.PanelType panelType)
    {
        var panel = GetWindow(panelType,null);
        if(panel != null)
        {
            GRoot.inst.HidePopup(panel);
        }
        return panel;   
    }
}